<?php
/**
 * Project: Recipe
 * File: FoodSectionAction.class.php
 * Author: Xnfy520@gmail.com
 * Date: 12-4-17
 * Time: 下午2:01
 */

	class FoodSectionAction extends CommonAction{

		function index(){

			import("ORG.Util.Page");
			$foodsection = D('FoodSection');

			if(isset($_POST['search'])){
				if(!empty($_POST['key'])){
					switch($_POST['search']){
						case 1 :
							$datas['id'] = $_POST['key'];
							break;
						case 2:
							$datas['name'] = array("like","%{$_POST['key']}%");
							break;
						case 3 :
							$datas['alias'] = array('like',"%{$_POST['key']}%");
							break;
					}
					$total = $foodsection->where($datas)->count();
					$Page = new Page($total, 7);
					$show = $Page->show();
					$list = $foodsection->relation(true)->where($datas)->limit($Page->firstRow.','.$Page->listRows)->order('id')->select();
				}else{
					$this->error('搜索内容不能为空', __APP__.'/FoodSection/index');
				}
			}else{
				$total = $foodsection->count();
				$Page = new Page($total, 7);// 实例化分页类 传入总记录数和每页显示的记录数
				$show = $Page->show();// 分页显示输出
				$list = $foodsection->relation(true)->limit($Page->firstRow.','.$Page->listRows)->order('id')->select();
			}

			$this->assign('list',$list);// 赋值数据集

			$this->assign('show',$show);// 赋值分页输出

			$this->display('index');
		}

		/**
		 * 指向添加食材栏目界面
		 */
		function add(){
			$this->display();
		}

		/**
		 * 添加食材栏目
		 */
		function insert(){
			$foodsection = D('FoodSection');
			$tmpdir = './Public/Content/tmp/';
			$srcdir = './Public/Content/FoodSection/';
			if($data = $foodsection->create()){
				if($foodsection->add()){
					if(!empty($data['image'])){
						copy($tmpdir.$data['image'], $srcdir.$data['image']);
						if($dirsrc = opendir($tmpdir)){
							while($files = readdir($dirsrc)){
								if($files!='.' && $files!='..'){
									$pathfile = $tmpdir.$files;
									if(is_file($pathfile)){
										unlink($pathfile);
									}
								}
							}
						}
					}
					$this->success('添加食材栏目成功', __APP__.'/FoodSection/index');
			}else{
					$this->error('添加食材栏目失败');
			}
		}else{
			$this->error($foodsection->getError(), __APP__.'/FoodSection/add');
		}
	}

		/**
		 *食材栏目封面上传
		 */
		function uploads(){
			import("ORG.Net.emit_upload");
			import("ORG.Util.emit_image");
			$upload = new upload(array("filepath"=>"./Public/Content/tmp","allowtype"=>array('gif','png','jpg'),"israndname"=>true,"maxsize"=>2000000));
			if($upload->uploadfiles('pic')){
				$img = new image("./Public/Content/tmp");
				$imgname =  $upload->getnewname();
				if($img->cut($imgname)){
					$img->thumb($imgname, 70, 70);
				}
				echo $upload->getnewname();
			}else{
				echo $upload->geterrormsg();
			}
		}

		/**
		 * 指向修改食材栏目界面
		 */
		function edit(){
			if(isset($_GET['id']) && !empty($_GET['id'])){
				$foodsection = M('FoodSection');
				$data = $foodsection->find($_GET['id']);
				$this->assign('data', $data);
				$this->display();
			}else{
				$this->error('异常操作');
			}
		}

		/**
		 * 修改食材栏目
		 */
		function update(){
			if(isset($_POST['id']) && !empty($_POST['id'])){
				$tmpdir = './Public/Content/tmp/';
				$srcdir = './Public/Content/FoodSection/';
				$foodsection = M('FoodSection');
				$foodsectioninfo = $foodsection->find($_POST['id']);
				if($data = $foodsection->create()){
					if($foodsection->save()){
						if($data['image']!=$foodsectioninfo['image']){
							unlink($srcdir.$foodsectioninfo['image']);
							copy($tmpdir.$data['image'], $srcdir.$data['image']);
							if($dirsrc = opendir($tmpdir)){
								while($files = readdir($dirsrc)){
									if($files!='.' && $files!='..'){
										$pathfile = $tmpdir.$files;
										if(is_file($pathfile)){
											unlink($pathfile);
										}
									}
								}
							}
						}
						$this->success('修改食材栏目成功',  __APP__.'/FoodSection/index');
					}else{
						$this->error('修改食材栏目失败');
					}
				}else{
					$this->error($foodsection->getError());
				}
			}else{
				$this->error('异常操作');
			}
		}

		/**
		 * 删除食材栏目
		 */
		function del(){
			if(!empty($_POST['actions'])){
				$srcdir = './Public/Content/FoodSection/';
				$foodsection = D('FoodSection');
				foreach($_POST['actions'] as $key=>$value){

					if($value==1){
						$this->error('调味品分类不能被删除');
					}else{
						$data = $foodsection->relation(true)->find($value);
						if(empty($data['foodcategory'])){
							$num = $foodsection->delete($value);
							unlink($srcdir.$data['image']);
						}else{
							$this->error('该食材栏目下还有食材分类');
						}
					}

				}
				if($num>0){
					$this->success('删除食材栏目成功', __APP__.'/FoodSection/index');
				}else{
					$this->error('删除食材栏目失败');
				}
			}else{
				$this->error('请先选择食材栏目');
			}
		}


	}